<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统管理 - 电动车销售系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/purchase.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/admin/adminlist.css">
</head>
<body>
<header class="dashboard-header">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h1 class="h3 mb-0"><i class="bi bi-bicycle"></i> 电动车销售管理系统</h1>
                <p class="mb-0 opacity-75">系统管理员工作台</p>
            </div>
            <div class="d-flex align-items-center">
                <div class="me-3">
                    <span class="d-none d-md-inline">${currentUser.name}</span>
                    <div class="badge bg-light text-dark">
                        <i class="bi bi-person-circle me-1"></i> 系统管理员
                    </div>
                </div>
                <div class="dropdown">
                    <button class="btn btn-light rounded-circle p-1" type="button" data-bs-toggle="dropdown">
                        <i class="bi bi-gear fs-5"></i>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-end">
                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/change_password.jsp"><i class="bi bi-lock me-2"></i> 修改密码</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="${pageContext.request.contextPath}/LogoutServlet"><i class="bi bi-box-arrow-right me-2"></i> 退出登录</a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</header>

<div class="container py-4">
    <div class="row">
        <!-- 客户管理板块 -->
        <div class="col-md-8 mb-4">
            <div class="dashboard-card shadow h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <div>
                        <h5 class="card-title"><i class="bi bi-people me-2 text-primary"></i>客户管理</h5>
                        <p class="card-subtitle">所有客户信息</p>
                    </div>
                    <div>
                        <a href="${pageContext.request.contextPath}/CustomerListServlet" class="btn btn-sm btn-primary">
                            <i class="bi bi-arrow-right me-1"></i> 管理客户
                        </a>
                    </div>
                </div>
                <div class="card-body p-0">
                    <div class="scrollable-container">
                        <c:choose>
                            <c:when test="${not empty allCustomers}">
                                <div class="row mx-0">
                                    <c:forEach items="${allCustomers}" var="customer">
                                        <div class="col-md-6 mb-3 px-2">
                                            <div class="card customer-card m-1">
                                                <div class="customer-info d-flex align-items-center p-3">
                                                    <div class="customer-avatar me-3">${fn:substring(customer.name, 0, 1)}</div>
                                                    <div style="overflow: hidden;">
                                                        <div class="customer-name fw-medium">${customer.name}</div>
                                                        <div class="customer-phone text-muted small"><i class="bi bi-telephone me-1"></i> ${customer.phone}</div>
                                                        <div class="customer-address text-muted small mt-1"><i class="bi bi-geo-alt me-1"></i> ${customer.address}</div>
                                                    </div>
                                                </div>
                                                <div class="customer-actions d-flex justify-content-end p-2 bg-light">
                                                    <a href="tel:${customer.phone}" class="btn btn-sm btn-outline-primary me-2">
                                                        <i class="bi bi-telephone"></i> 联系
                                                    </a>
                                                </div>
                                            </div>
                                        </div>
                                    </c:forEach>
                                </div>
                            </c:when>
                            <c:otherwise>
                                <div class="empty-state py-4 text-center">
                                    <i class="bi bi-people fs-1 text-muted"></i>
                                    <p class="mt-3">暂无客户数据</p>
                                    <div class="alert alert-warning mt-3">
                                        <i class="bi bi-exclamation-circle me-2"></i> 客户数据加载失败或为空
                                    </div>
                                </div>
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>
                <div class="card-footer bg-white py-2">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="text-muted small">
                            <c:choose>
                                <c:when test="${not empty allCustomers}">
                                    共有 ${fn:length(allCustomers)} 位客户
                                </c:when>
                                <c:otherwise>
                                    暂无客户数据
                                </c:otherwise>
                            </c:choose>
                        </div>
                        <a href="${pageContext.request.contextPath}/addcustomer" class="btn btn-sm btn-outline-primary">
                            <i class="bi bi-person-plus me-1"></i> 添加新客户
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- 用户管理 -->
        <div class="col-md-4 mb-4">
            <div class="dashboard-card shadow h-100">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <div>
                        <h5 class="card-title"><i class="bi bi-person-gear me-2 text-primary"></i>用户管理</h5>
                        <p class="card-subtitle">所有系统用户账户</p>
                    </div>
                    <div>
                        <a href="${pageContext.request.contextPath}/UserListServlet" class="btn btn-sm btn-primary">
                            <i class="bi bi-arrow-right me-1"></i> 管理用户
                        </a>
                    </div>
                </div>
                <div class="card-body p-0">
                    <div class="scrollable-container">
                        <c:choose>
                            <c:when test="${not empty allUsers}">
                                <div class="list-group list-group-flush">
                                    <c:forEach items="${allUsers}" var="user">
                                        <div class="list-group-item user-item">
                                            <div class="d-flex align-items-center">
                                                <div class="role-badge
                                                    <c:choose>
                                                        <c:when test="${user.role == 4}">role-admin</c:when>
                                                        <c:when test="${user.role == 0}">role-sales</c:when>
                                                        <c:when test="${user.role == 1}">role-warehouse</c:when>
                                                        <c:when test="${user.role == 2}">role-salesadmin</c:when>
                                                        <c:when test="${user.role == 3}">role-aftersales</c:when>
                                                    </c:choose>">
                                                    <c:choose>
                                                        <c:when test="${user.role == 4}">管理员</c:when>
                                                        <c:when test="${user.role == 0}">业务员</c:when>
                                                        <c:when test="${user.role == 1}">仓库</c:when>
                                                        <c:when test="${user.role == 2}">销售</c:when>
                                                        <c:when test="${user.role == 3}">售后</c:when>
                                                    </c:choose>
                                                </div>
                                                <div style="overflow: hidden;">
                                                    <div class="fw-medium">${user.name}</div>
                                                    <div class="text-muted small">
                                                        <c:choose>
                                                            <c:when test="${user.role == 4}">系统管理员</c:when>
                                                            <c:when test="${user.role == 0}">进车管理</c:when>
                                                            <c:when test="${user.role == 1}">库存管理</c:when>
                                                            <c:when test="${user.role == 2}">销售管理</c:when>
                                                            <c:when test="${user.role == 3}">售后服务</c:when>
                                                        </c:choose>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </c:forEach>
                                </div>
                            </c:when>
                            <c:otherwise>
                                <div class="empty-state py-4 text-center">
                                    <i class="bi bi-person-lines-fill fs-1 text-muted"></i>
                                    <p class="mt-3">暂无用户数据</p>
                                    <div class="alert alert-warning mt-3">
                                        <i class="bi bi-exclamation-circle me-2"></i> 用户数据加载失败或为空
                                    </div>
                                </div>
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>
                <div class="card-footer bg-white py-2">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="text-muted small">
                            <c:choose>
                                <c:when test="${not empty allUsers}">
                                    共有 ${fn:length(allUsers)} 位用户
                                </c:when>
                                <c:otherwise>
                                    暂无用户数据
                                </c:otherwise>
                            </c:choose>
                        </div>
                        <a href="${pageContext.request.contextPath}/AddUserServlet" class="btn btn-sm btn-outline-primary">
                            <i class="bi bi-person-plus me-1"></i> 添加新用户
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 报表统计模块 - 优化后 -->
    <div class="row">
        <div class="col-12 mb-4">
            <div class="dashboard-card shadow">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <div>
                        <h5 class="card-title"><i class="bi bi-bar-chart me-2 text-primary"></i> 报表统计</h5>
                        <p class="card-subtitle">系统数据统计与分析</p>
                    </div>
                    <div>
                        <a href="${pageContext.request.contextPath}/admin/report.jsp" class="btn btn-sm btn-primary">
                            <i class="bi bi-arrow-right me-1"></i> 查看报表
                        </a>
                    </div>
                </div>

                <div class="stat-container">
                    <div class="stat-item">
                        <div class="stat-icon sales">
                            <i class="bi bi-graph-up"></i>
                        </div>
                        <div class="stat-value">
                            <fmt:formatNumber value="${saleCompletionRate}" type="percent" maxFractionDigits="0"/>
                        </div>
                        <div class="stat-label">销售完成率</div>
                    </div>

                    <div class="stat-item">
                        <div class="stat-icon orders">
                            <i class="bi bi-cart-check"></i>
                        </div>
                        <div class="stat-value">${weeklyOrders}</div>
                        <div class="stat-label">本周订单</div>
                    </div>

                    <div class="stat-item">
                        <div class="stat-icon service">
                            <i class="bi bi-tools"></i>
                        </div>
                        <div class="stat-value">${pendingAfterSales}</div>
                        <div class="stat-label">待处理售后</div>
                    </div>
                </div>

                <div class="progress-section">
                    <div class="progress-title">
                        <span>库存周转率</span>
                        <span><fmt:formatNumber value="${inventoryTurnover}" type="percent" maxFractionDigits="0"/></span>
                    </div>
                    <div class="custom-progress">
                        <div class="custom-progress-bar" style="width: <fmt:formatNumber value="${inventoryTurnover * 100}" maxFractionDigits="0"/>%"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <c:if test="${not empty successMsg}">
        <div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
            <i class="bi bi-check-circle me-2"></i> ${successMsg}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    </c:if>

    <c:if test="${not empty errorMsg}">
        <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
            <i class="bi bi-exclamation-triangle me-2"></i> ${errorMsg}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    </c:if>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 自动关闭消息提示
    document.querySelectorAll('.alert .btn-close').forEach(button => {
        button.addEventListener('click', function() {
            this.closest('.alert').style.display = 'none';
        });
    });

    // 5秒后自动关闭消息提示
    setTimeout(() => {
        document.querySelectorAll('.alert').forEach(alert => {
            alert.style.display = 'none';
        });
    }, 5000);

    document.addEventListener('DOMContentLoaded', function() {
        const allCustomersEmpty = ${empty allCustomers};
        const allUsersEmpty = ${empty allUsers};

        if (allCustomersEmpty || allUsersEmpty) {
            const reloadBtn = document.createElement('button');
            reloadBtn.className = 'btn btn-outline-primary mt-3';
            reloadBtn.innerHTML = '<i class="bi bi-arrow-repeat me-1"></i> 重新加载数据';
            reloadBtn.onclick = function() {
                window.location.href = '${pageContext.request.contextPath}/AdminDashboardServlet';
            };

            document.querySelectorAll('.alert-warning').forEach(alert => {
                alert.appendChild(reloadBtn);
            });
        }

        // 添加进度条动画
        const progressBar = document.querySelector('.custom-progress-bar');
        const computedWidth = progressBar.style.width;
        progressBar.style.width = '0';
        setTimeout(() => {
            progressBar.style.width = computedWidth;
        }, 300);
    });
</script>
</body>
</html>